<template>
  <div class="login-container">
    <div class="divContent">
      <div class="divLeft">
        <img class="imgs" src="http://www-wms-java.itheima.net/img/title.ef005a7a.png" alt="">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

          <el-form-item prop="username" class="margin">
            <div class="txt-img"> <img src="../../assets/images/下载 (1).png" alt=""> </div>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <div class="txt-img"> <img src="../../assets/images/小锁头.png" alt=""> </div>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <i class="el-icon-view" />
          </el-form-item>
          <el-checkbox-group v-model="isboor">
            <el-checkbox label="记住密码" name="type" />
          </el-checkbox-group>
          <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">立即登录</el-button>
        </el-form>
      </div>
      <div class="divRight" />
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      isboor: '',
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$router.push('/dashboard')
          // this.loading = true
          // this.$store.dispatch('user/login', this.loginForm).then(() => {
          //   this.$router.push({ path: this.redirect || '/' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container{
  height: 100%;
  background-color: #fff;
}
.divContent{

  width: 958px;
    height: 516px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    box-shadow: 0 0 20px rgb(93 93 93 / 33%);
    // background-color: black;
    border-radius: 40px;
    .imgs{
      width: 150px;
    height: 64px;
    margin-top: 42px;
    margin-left: 94px;
    margin-bottom: 50px;
    }
    .divLeft{
      float: left;
    width: 398px;
    height: 516px;
    background: #fff;
    border-radius: 34px 0 0 34px;
    margin: 0 auto;
    padding: 30px;
    }
    .divRight{
      float: right;
    background-image: url(http://www-wms-java.itheima.net/img/contentBg.1321d126.png);
    background-position: 50%;
    background-repeat: no-repeat;
    background-size: cover;
    width: 560px;
    height: 516px;
    }
    .txt-img{
      float: left;
    width: 50px;
    height: 50px;
    background: #eae7e7;
    border-radius: 8px 0 0 8px;
      img{
        width: 50%;
        height: 50%;
        margin-top: 12px;
        margin-left: 12px;
      }
    }
}
.el-input{
  width: 230px;
    height: 50px;
    background-color: #f8f5f5;
    border-bottom-left-radius: 0;
    border-top-left-radius: 0;
    border-radius:5px;
}
::v-deep .el-input__inner{
  height: 49px;
    // line-height: 60px !important;
    border: 1px solid #f8f5f5;
    background: #f8f5f5;
    border-radius: 6px;

}
.el-form-item{
  display: flex;
  justify-content: space-between;
}
.margin{
  margin-bottom: 43px;
}
.el-checkbox-group{
  margin-left: 30px;
  margin-bottom: 24px;
}
.el-button {
  font-size: 16px;
  box-shadow: 0 2px 9px 1px rgb(255 178 0 / 47%);
  border-radius: 8px;
  width: 278px;
  height: 48px;
  font-weight: 600;
  margin-left: 30px;
  color: #000;
  line-height: 17px;
}
.el-icon-view{
  position: absolute;
  top: 17px;
  color: #c0c4cc;
  right: 10px;
}
</style>
